package cc.monggo.web.controller.register;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cc.monggo.common.AbstractBaseController;
import cc.monggo.common.Constants;
import cc.monggo.domain.BaseUser;
import cc.monggo.utils.EncrypTool;
import cc.monggo.web.form.RegisterForm;
import cc.monggo.web.form.UserForm;

/**
 * 用户注册
 * 
 * @author fangjinsong
 * 
 */
@Controller
@RequestMapping("/register")
public class RegisterController extends AbstractBaseController {

	@RequestMapping(value = "index")
	public ModelAndView index() {
		return null;
	}

	//---------- 第三方注册页 ----------

	/**
	 * 注册页
	 * 
	 * @param req
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/third")
	public ModelAndView thirdRegister(HttpServletRequest request,
			HttpServletResponse response, RegisterForm form) throws Exception {
		ModelAndView mv = new ModelAndView("/register/third/register",Constants.FORM_COMMAND_KEY, form);
		return mv;
	}
	
	/**
	 * 注册
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/third/doRegister")
	public ModelAndView doThirdRegister(HttpServletRequest request,
			HttpServletResponse response, RegisterForm form) throws Exception {
		ModelAndView mv = new ModelAndView("/register/third/bind_success",Constants.FORM_COMMAND_KEY, form);
		form.setThirdId(EncrypTool.dencode(form.getThirdId()));
		form.form2BaseUser();
		form.form2XThirdUser();
		BaseUser baseUser = registerService.insertThirdUser(form.getxThirdUser(),form.getBaseUser());
		
		StringBuffer bf = new StringBuffer();
		bf.append("redirect:/register/third/success")
		.append("?userId=").append(EncrypTool.encode(baseUser.getUserId()+""))
		.append("&userName=").append(baseUser.getUserName())
		.append("&type=").append(form.getType())
		.append("&nickname=").append(form.getNickname());
		
		mv = new ModelAndView(bf.toString());	
		return mv;
	}
	
	/**
	 * 注册成功页
	 * 
	 * @param request
	 * @param response
	 * @param form
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/third/success")
	public ModelAndView doThirdRegisterSuccess(HttpServletRequest request,
			HttpServletResponse response, RegisterForm form) throws Exception {
		ModelAndView mv = new ModelAndView("/register/third/bind_success",Constants.FORM_COMMAND_KEY, form);
		return mv;
	}

}
